<?php

/**
 * page products
 * @author Tien Nguyen
 */
class ProductsController extends Zend_Controller_Action {

    /**
     * Model for Products
     * @var Application_Model_Products $_modelProducts 
     */
    protected $_modelProducts;
    
    /**
     * Model for ProductCategories
     * @var Application_Model_ProductCategories
     */
    protected $_modelProductCategories = null;

    /**
     * Function instance class 
     * @author Tomni Tien
     */
    public function init() {
        parent::init();
        $this->_modelProducts = new Application_Model_Products();
        $this->_modelProductCategories = new Application_Model_ProductCategories();
    }

    /**
     * Action index default in controller
     * @author Tomni Tien
     */
    public function indexAction() {
        
    }

    /**
     * Action show list from product last, this function for index controller
     * Show number of rows is params from get paramatter of request
     * @author Tomni Tien
     */
    public function showListLastProductsAction() {
        //Get param get number row per page and convert to integer
        $numberRowPerPage = (int) $this->_request->getParam('number-row-per-page', Zend_Registry::get('config')->rows_per_one_page);

        //Get select for products from model product
        $selectProducts = $this->_modelProducts->getListLastProducts();

        //Paging
        $paginator = Zend_Paginator::factory($selectProducts);
        //Get config for number row in page
        $paginator->setItemCountPerPage($numberRowPerPage);
        $paginator->setPageRange(Zend_Registry::get('config')->number_page_in_paging);

        //Set view
        $this->view->listProducts = $paginator;
    }

    /**
     * Show list hot product
     * @author Tien Nguyen
     */
    public function showListHotProductsAction() {
        //Get param get number row per page and convert to integer
        $numberRowPerPage = (int) $this->_request->getParam('number-row-per-page', Zend_Registry::get('config')->rows_per_one_page);

        //Get select for products from model product
        $selectProducts = $this->_modelProducts->getListHotProducts();

        //Paging
        $paginator = Zend_Paginator::factory($selectProducts);
        //Get config for number row in page
        $paginator->setItemCountPerPage($numberRowPerPage);
        $paginator->setPageRange(Zend_Registry::get('config')->number_page_in_paging);

        //Set view
        $this->view->listProducts = $paginator;
    }

    /**
     * Action show detail product 
     * @author Tien Nguyen
     */
    public function showDetailProductAction() {
        //Get product id
        $productId = $this->_request->getParam('product-id');

        //Get product
        $product = $this->_modelProducts->getProduct($productId);

        //set view product
        $this->view->product = $product;
    }

    public function showProductCategoryAction() {
        //get category id
        $categoryId = $this->_request->getParam('category-id');
        $numberRowPerPage = (int) $this->_request->getParam('number-row-per-page', Zend_Registry::get('config')->rows_per_one_page);
        
        //get page 
        $page = $this->_request->getParam('page', 1);

        //check category id
        if (!empty($categoryId)) {
            //category
            $category = $this->_modelProductCategories->find($categoryId)->current();
            
            $selectProduct = $this->_modelProducts->getListProductCategory($categoryId);
            
            //paging 
            $paginator = Zend_Paginator::factory($selectProduct);
            //Get config for number row in page
            $paginator->setItemCountPerPage($numberRowPerPage);
            $paginator->setCurrentPageNumber($page);
            $paginator->setPageRange(Zend_Registry::get('config')->number_page_in_paging);

            $this->view->products = $paginator;
            $this->view->category = $category;
        } else {
            $this->_redirect('/index');
        }
    }

}
